#!/usr/bin/env ruby
#Command Line Shell written in ruby! This is meant to be used on an IT locked down systems!
#It has some beginner functions, like the games menu, and can work on all OS's... but 
#it is ment for IT locked down systems windowz.!!! This is for locked down systems, NOT  a shell replacement...
#I have no clue why anyone would use it on *nix but its built in if they do. 
#The /dev/null crap is to find out what system we're on...
#if echoing to /dev/null returns true, then were on *nix, if false, it windowz
#The loop at the bottom goes through and calls each method, input -> test -> theCommand
#The loop gets the user's command, sees if its a keyword, does some voodo, then executes it! 

#HowTo:
#1. Download a portable version of ruby with irb. Install on thumbdrive.
#2. Open IRB
#3. Type this command: system("ruby rash.rb")
#4. You will now be in rash!

#Google code doesn't like =begin and =end so I had to use #...
#Copyright 2008® Mike Napolitano under the new BSD license. All Rights Reserved. 

class CommandLine #This is where ALL the magic happens
attr_accessor :command, :osCommand, :os, :version #Set our setters and geters in one easy line! I <3 ruby
	def initialize #This gets called when the Class is instantiated, so this always happens automatically.
		testSystem = system("echo "" > /dev/null") #Find if were on *nix or windowz, by testing if it returns false
		if testSystem == true #if thats a real command, were *nix!
			@osCommand,@os = "echo "" > /dev/null","*nix" #the do nothing command is set, and the OS is set
		else
			@osCommand,@os = "echo "" > NUL","windowz" #if /dev/null returns false, use NUL and windowz
		end
    @version = "1.0b"
	end
		
	def input #Prints the Rash prompt and gets input
		print "#{Dir.getwd}: Rash$ "
		@command = gets.chomp #puts input into @command without the trailing \n 
	end
	
	def tests #tests the input...
	  if @command == "" #If blank, set command to test command, so we won't error
	    @command = @osCommand
    end
    
		case @command.split[0].downcase  #Case is like switch in C... this part is pretty importaint!
			when "exit", "quit"
				Process.exit!
				
			when "pushd" #pushd wasn't working...
				puts "Please use cd instead"
				@command = @osCommand
				
			when "games","-games" 
				self.games

			when "Rash","about"
				self.about
				
			when "version","--version","-v"
			  self.version
			
			when "help","--help","-h"
			  self.help
			  
			when "for","if","case" #Scripting is NOT supported at all...
			  	if @os == "*nix"
			  	  puts "Sorry... no shell scripting support... why aren't you using Bash or ZSH???"
		  	  else	puts "Sorry... no shell scripting support..."
	  	    end
			  	@command = @osCommand
			  	
			when "cd" #cd wasn't working right, so I used the Dir.chdir! 
				if @command.split[1] == nil #Dies ugly if there is no argument!
					puts "cd requires and argument"
				else #if there is an arg test to see if its ligit
					begin #If the path is wrong, it dies VERY ungracefully... so start a begin, which starts a try/rescue
						Dir.chdir("#{@command.split[1]}") #This does the actual cd, but if it fails...
					rescue #rescue so we don't get a horrible error!
						puts "No directory by that name..." #print error
					end
				end
				@command = @osCommand #Because were doing the command HERE, set @command to the null thing
		end
	end
	
	def theCommand #Does the command... 
		s = system("#{@command}") #sets the return value to "s"
		if s == false #IF it returns false, its MOST LIKELY not a real command
			puts "That command doesn't exist!"
		end
	end
	
  ######################################################################
	def games #Games menu for windowz. Quick launch of your favorite game!
	  if @os == "windowz"
	    puts "Enter the number of the game you would wish to play!"
	    puts "[1] Solitaire"
	    puts "[2] Spider Solitaire"
	    puts "[3] Hearts"
	    puts "[4] Freecell"
	    game = gets.chomp #set choice of game
	    
	    case game #If the game variable == ...
        when "1","sol","solitaire" # 1 or sol or solitaire...
          @command = "sol" #set the command to 'sol' which means launch solitaire
        when "2","spider","spidersolitaire","spider solitaire" #rather, rince repeat
          @command = "spider"
        when "3","hearts"
          @command = "hearts"
        when "4","freecell","free cell"
          @command = "freecell"
        else #set the command to the test one above
          @command = @osCommand
      end
      else
        puts "Sorry... windowz only... ='("
        @command = @osCommand          
    end  
  end
	
	######################################################################
	def about #When called, prints about
	  puts "Rash - cmd.exe replacement for Windows. Works on *nix too (proof of concept)"
	  puts "Type -h for help and -v for version"
	  @command = @osCommand
  end
  
  def help
    puts "Rash - cmd.exe replacement!"
    puts "help : prints help"
    puts "version: prints version"
    puts "about: prints about"
    puts "games: prints game menu (only on windows) "
    puts "exit or quit: exit or quit..."
    @command = @osCommand
  end
  
  def version
    puts "Rash Version: #{@version}"
    @command = @osCommand
  end
  
end #END OF CLASS!!!

cmd = CommandLine.new #INSTANTIATE!!!
puts "Welcome to Rash -- Command Line by Moo!" #Prints welcome once

loop do #main program loop
	begin #Start our loop...
		cmd.input #GET INPUT
		cmd.tests #PROCESS INPUT
		cmd.theCommand #EXECUTE INPUT!!!
	end #Lets do it again!!!!
end #Lets die peacefully!

#The internet is like a series of tubes...


		
		
